package com.hna.face.controller.business;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.hna.face.base.BaseController;
import com.hna.face.common.result.BaseResult;
import com.hna.face.model.FacePerson;
import com.hna.face.model.FaceSyncInfo;
import com.hna.face.model.PageInfo;
import com.hna.face.service.IFacePersonService;
import com.hna.face.service.IFaceScheduleService;
import com.hna.face.service.IFaceSyncInfoService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;


@Controller
public class PersonController extends BaseController {
	@Autowired 
	IFacePersonService facePersonService;
	
	@Autowired
	IFaceScheduleService faceScheduleService;
	
	@Autowired
	IFaceSyncInfoService faceSyncInfoService;

    
    private final static Logger log = LoggerFactory.getLogger(PersonController.class);
    

    @RequestMapping("/person/index")
    public String index() {
        return "pages/person/index";
    }
    
    	@RequestMapping("/person/add")
    public String add() {
        return "pages/person/add";
    }



	@RequestMapping("/person/gofaceto.do")
	public String gofaceto(HttpServletRequest request,String name) {
		request.setAttribute("name", name);
		return "pages/face/index";
	}

//    @RequestMapping("/person/detail")
//    public ModelAndView details(HttpServletRequest request) {
//        ModelAndView modelAndView = new ModelAndView("pages/person/detail");
//        modelAndView.addObject("id", request.getParameter("id"));
//        return modelAndView;
//    }
//    
//    
//    @RequestMapping("/person/edit")
//	public ModelAndView edit(HttpServletRequest request) {
//		ModelAndView modelAndView = new ModelAndView("pages/person/edit");
//		modelAndView.addObject("id", request.getParameter("id"));
//		return modelAndView;
//	}

   
    
    @RequestMapping(value = { "/rest/person/list" }, method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> list(HttpServletRequest request, PageInfo pageInfo,FacePerson person) {
        Map<String, Object> res = new HashMap<>();
        try {
            Wrapper<FacePerson> wrapper=new EntityWrapper<>();
            if(StringUtils.isNotBlank(person.getName())){
        		wrapper.like("name", person.getName());
            }
            if(StringUtils.isNotBlank(person.getEmployeeNo())) {
            	wrapper.eq("employee_no", person.getEmployeeNo());
            }
            if(StringUtils.isNotBlank(person.getDeptment())) {
            	wrapper.eq("deptment", person.getDeptment());
            }
//            if(StringUtils.isNotBlank(person.getMasterName())){
//        		wrapper.like("master_name", person.getMasterName());
//            }
            Page<FacePerson> page = new Page<>(pageInfo.getPage(),pageInfo.getRows());
            Page<FacePerson> personList = facePersonService.selectPage(page, wrapper);
            
            res.put("rows", personList.getRecords());
            res.put("total", personList.getTotal());
            res.put("success", "true");
            return res;
        } catch (Exception e) {
            res.put("success", "false");
            return res;
        }
    }
    
    @RequestMapping("/rest/person/info")
    public @ResponseBody
    BaseResult info(HttpServletRequest request) {
            Long id=Long.parseLong( request.getParameter("id"));
            FacePerson person = facePersonService.selectById(id);
            return BaseResult.success(person);
    }
    
    @RequestMapping(value = { "/rest/person/del" }, method = RequestMethod.POST)
    public @ResponseBody
    BaseResult del(HttpServletRequest request,String ids) {
        try {
        	if(StringUtils.isEmpty(ids)){//数据不为空
        		return BaseResult.success();
        	}
        	List<String> asList = Arrays.asList(ids.split(","));//分割
			facePersonService.deleteBatchIds(asList);//删除集合
			for (String id : asList) {//更新同步表
				this.syninfo(Long.valueOf(id),2);
			}
            return BaseResult.success();
        } catch (Exception e) {
        	log.error("发生异常了",e);
            return BaseResult.fail(e.getMessage());
        }
    }
    
    
    @RequestMapping(value = { "/rest/person/save" }, method = RequestMethod.POST)
    public @ResponseBody
    BaseResult save(HttpServletRequest request,HttpServletResponse response, String name,String deptment,String employeeNo) {
        Map<String, Object> res = new HashMap<>();
        try {
        	FacePerson person = new FacePerson();
        	person.setName(name);
        	person.setCreateTime(new Date());
        	person.setDeptment(deptment);
        	person.setEmployeeNo(employeeNo);
        	facePersonService.insert(person);
        	
        	this.syninfo(person.getId(), 1);
            
        	return BaseResult.success();
        } catch (Exception e) {
        	log.error("发生异常了",e);
            return BaseResult.fail(e.getMessage());
        }
    }
    
    
    @RequestMapping(value = { "/rest/person/update" }, method = RequestMethod.POST)
    public @ResponseBody
    BaseResult update(HttpServletRequest request, FacePerson person,String meterNo) {
        Map<String, Object> res = new HashMap<>();
        try {
//        	person.setMeterNo(meterNo);
        	facePersonService.updateById(person);
        	
        	this.syninfo(person.getId(), 3);
        	
//        	MjMeter meter = new MjMeter();
//        	meter.setpersonId(person.getId());
//        	meter.setInstallTime(person.getRegisteredTime());
//        	meter.setMeterId(meterId);
//			meterService.insert(meter );
            
        	return BaseResult.success();
        } catch (Exception e) {
        	log.error("发生异常了",e);
            return BaseResult.fail(e.getMessage());
        }
    }
    private void syninfo(Long id,Integer opt) {
        log.info("syninfo:"+opt);
    	/*Wrapper<FaceSchedule> wrapper = new EntityWrapper<>();
		wrapper.eq("person_id", id);
		FaceSchedule faceSchedule = this.faceScheduleService.selectOne(wrapper);
        log.info("faceSchedule:"+faceSchedule);*/
		FaceSyncInfo faceSyncInfo = new FaceSyncInfo();
        faceSyncInfo.setTableName("face_person");
        faceSyncInfo.setRecordId(id);
        faceSyncInfo.setOpt(opt);
        faceSyncInfo.setStatus(1);
        faceSyncInfo.setCreateTime(new Date());
       // faceSyncInfo.setVehicleId(faceSchedule.getVehicleId());
       // faceSyncInfo.setVehicleNo(faceSchedule.getVehicleNo());
        boolean insert = this.faceSyncInfoService.insert(faceSyncInfo);
        log.info("faceSyncInfo:"+insert);
    }
  
}


 